#!/bin/sh
# shellcheck disable=SC2039
# shellcheck disable=SC1090
source ~/.bash_profile

GIT_BRANCH="release"
# shellcheck disable=SC2088
WORKSPACE='workspace/jn-cloud-manage'
mkdir -p ${WORKSPACE}
cd ${WORKSPACE}

# shellcheck disable=SC2068
SERVER_GIT_URLS=(
  https://codeup.aliyun.com/jiniutech/jnsk-cloud/ums-user-server.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-user-act-server.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-partner-back.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-task-server.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-data-etl.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-back.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-venus.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-risk.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-crash.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-sms-server.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/sms-middleware-server.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-pay-server.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-pay-tool-server.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-user-mask.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-user-server.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-product.git
#  https://codeup.aliyun.com/jiniutech/jnsk-cloud/cloud-upc.git
)

URL_PREFIX_LEN=`echo 'https://codeup.aliyun.com/jiniutech/jnsk-cloud/' | wc -m`
# shellcheck disable=SC2068
for GIT_URL in ${SERVER_GIT_URLS[@]}; do
  SM_GIT_URL=`echo ${GIT_URL//.git/}` 
  SERVER_NAME=`echo ${SM_GIT_URL: URL_PREFIX_LEN - 1}`
  if [ ! -d ${SERVER_NAME} ]; then
    git clone -b ${GIT_BRANCH} ${GIT_URL}
  fi

  # shellcheck disable=SC2164
  cd "${SERVER_NAME}"
  REMOTE_BRANCHS=`git branch -r`
  for REMOTE_BRANCH in ${REMOTE_BRANCHS[@]}; do
    REMOTE_BRANCH=`echo ${REMOTE_BRANCH: 7}`
    if [ "${REMOTE_BRANCH}" != "release" -a "${REMOTE_BRANCH}" != "master" -a "${REMOTE_BRANCH}" != "HEAD" -a "${REMOTE_BRANCH}" != "" ]; then
      echo "${REMOTE_BRANCH}" 
      # git push origin --delete ${REMOTE_BRANCH}
    fi
  done
  cd ..
done